<?php
/**
 * Created by PhpStorm.
 * User: madong
 * Date: 2018/10/23
 * Time: 8:26 PM
 */

namespace app\api\controller\jiaoyi;


use think\Controller;
use think\Db;

class Coinpai extends Controller
{
    //排队转账

    private $zongqianbao = '0x50f1Ca5621496A700FB7042c77E3Fd6d5c62427A';//总钱包地址

    private $zongqianbaokey = '0xebd3d69c9461354a49283eab33bc082ff73499dc85a93606ec517d0308ff08cd';//总钱包私钥

    private $host_ip = '47.92.195.55';//钱包服务器IP

    public function Zhuanzhangpai_SfrVpH9rxR9Pvgw0UxUCvc9APobdaAFCrw6qVjuPhnHCSzwntrgBz0vMWyFR0uCU(){

        set_time_limit(0);  //无限制时间执行

        $data = Db::name('jiaoyiqianbaolog')->where('jiaoyitype',1)->where('jiaoyizt',3)->limit(50)->select();

        if ($data){

            foreach ($data as $value){

                $nonce = Db::name('jiaoyinonce')->where('id',1)->value('nonce');

                $nonce1 = $nonce+1;

                $fasong = json_decode(file_get_contents('http://'.$this->host_ip.':8860/?type=3&from='
                    .$this->zongqianbao.'&privateKey='.$this->zongqianbaokey.'&to='.$value['to'].'&value='.floatval($value['jiaoyinum']-0.0005).'&anonce='.(int)$nonce1))->hash;


                if (strlen($fasong)<60){

                    echo $value['to'].' ---------- 转账失败';echo("<hr>\r\n");

                }else{

                    if (Db::name('jiaoyiqianbaolog')->where('id',$value['id'])->update(['hash'=>$fasong,'nonce'=>$nonce1,'jiaoyizt'=>2])){

                        $user = Db::name('jiaoyiqianbaolog')->where('id',$value['id'])->value('uname');

                        Db::startTrans();

                        try{

                            Db::name('jiaoyizhuanchudj')->where('uname',$user)->where('zt',0)->update([

                               'hash' => $fasong,

                                'djsfnote' => '提现成功释放冻结',

                                'djsfsj' => time(),

                                'zt' => 1,

                            ]);

                            Db::name('jiaoyinonce')->where('id',1)->setInc('nonce',1);

                            Db::commit();

                            echo $value['to'].' ---------- 转账成功';echo("<hr>\r\n");

                        } catch (\Exception $e) {

                            echo $value['to'].' ---------- 转账成功,更新冻结状态和nonce增加失败';echo("<hr>\r\n");

                            Db::rollback();
                        }

                    }else{

                        echo $value['to'].' ---------- 转账成功 ---------- 哈希值更新失败';echo("<hr>\r\n");

                        Db::name('jiaoyinonce')->where('id',1)->setInc('nonce',1);


                    }


                }

            }

        }else{

            echo '暂时没有待处理的订单';


        }







        //dump($hash);

    }

}